package com.fundodoo.sfe.common.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fundodoo.sfe.common.po.PermissionInfo;
import com.fundodoo.sfe.domain.User;

/**
 * 
 * @author https://www.fundodoo.com
 *
 */
public class BaseController {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
    protected static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    protected User getCurrentUser() {
    	Subject subject = getSubject();
    	Object principal = subject.getPrincipal();
    	if(principal instanceof User) {
    		return (User)principal;
    	}else {
    		return null;
    	}
    }
    
    protected Session getSession() {
        return getSubject().getSession();
    }

    protected Session getSession(Boolean flag) {
        return getSubject().getSession(flag);
    }

    protected void login(AuthenticationToken token) {
        getSubject().login(token);
    }
    
    protected PermissionInfo getCurrentUserPermissionInfo() {
        User user = getCurrentUser();
        if(null != user)
        	return user.getPermissionInfo();
        return new PermissionInfo();
    }
}
